<?php

/**********************************************************************
Copyright (C) FrontAccounting, LLC.
Released under the terms of the GNU General Public License, GPL,
as published by the Free Software Foundation, either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/
include_once ($path_to_root . "/manufacturing/includes/db/work_centres_db.inc");
include_once ($path_to_root . "/manufacturing/includes/db/work_orders_db.inc");
include_once ($path_to_root .
    "/manufacturing/includes/db/work_orders_quick_db.inc");
include_once ($path_to_root .
    "/manufacturing/includes/db/work_order_issues_db.inc");
include_once ($path_to_root .
    "/manufacturing/includes/db/work_order_produce_items_db.inc");
include_once ($path_to_root .
    "/manufacturing/includes/db/work_order_requirements_db.inc");


function get_production_card($type, $wo_id)
{
    //$sql = "select * from ".TB_PREF."production_card left join employees on production_card.employeeid = employees.empid where production_card.typeid = ".db_escape($type). " and wo_id = ".db_escape($wo_id);
    $sql = "SELECT *
	FROM
	" . TB_PREF . "production_card
	LEFT JOIN " . TB_PREF . "employees
	ON production_card.employeeid = employees.empid
	LEFT JOIN " . TB_PREF . "item_codes
	ON production_card.item_id = item_codes.item_code
	WHERE
	production_card.typeid = " . db_escape($type) . "
	AND wo_id = " . db_escape($wo_id);
    return db_query($sql);
}

function del_production_card($sel_id)
{
    $sql = "DELETE FROM
	" . TB_PREF . "production_card
	WHERE cardid = " . db_escape($sel_id);
    db_query($sql);
}

function update_production_card_item($date, $sel_id)
{
    $sql = "UPDATE " . TB_PREF . "production_card SET
	waktu_selesai = " . db_escape(datetime2sql($date)) . " WHERE
	cardid = " . db_escape($sel_id);
    db_query($sql);
}

function close_production_card($wo_item)
{
    $sql = "UPDATE " . TB_PREF . "sales_order_details set
	isclose = 1, qty_sent = quantity where ref = " . db_escape($wo_item) . "
	";
    db_query($sql);
}

function get_production_card_item($sel_id)
{
    $sql = "SELECT *
	FROM
	" . TB_PREF . "production_card
	LEFT JOIN " . TB_PREF . "employees
	ON production_card.employeeid = employees.empid
	LEFT JOIN " . TB_PREF . "item_codes
	ON production_card.item_id = item_codes.item_code
	WHERE production_card.cardid = " . db_escape($sel_id);
    $result = db_query($sql);
    return db_fetch($result);
}

function add_production_card($type, $dtTerima, $itemId, $itemAmount, $empId, $wo_id)
{
    $sql = "INSERT INTO " . TB_PREF . "production_card (typeid, waktu_diterima,
	item_id, item_amount, employeeid, wo_id) VALUES (" . db_escape($type) . ", " .
        db_escape(datetime2sql($dtTerima)) . ",
	" . db_escape($itemId) . ", " . db_escape($itemAmount) . "," . db_escape($empId) .
        ", " . db_escape($wo_id) . ")";
    db_query($sql);
}


function get_stock_unit($id_comp)
{
    $sql = "SELECT units FROM stock_master where stock_id = ".db_escape($id_comp);
    $result = db_query($sql);
    return db_result($result);
}

function get_production_card_array($type, $wo_id)
{
    $arr = array();
    $result = get_production_card($type, $wo_id);
    while ($myrow = mysql_fetch_array($result))
    {
        array_push($arr, $myrow);
    }
    return $arr;
}

function get_sales_order_item_details_sum_pesanan($soid_ref)
{
    $sql = "SELECT CONCAT('S ',s.size_s,' M ',s.size_m,' L ',s.size_l,' XL ',s.size_ml,
	' XXL ',s.size_xxl,' XXXL ',s.size_xxxl,' 3L ',s.size_3l,' Lainnya ',s.size_other) AS jumlah_pesanan FROM
	" . TB_PREF . "sales_order_item_details s where id = " . db_escape($soid_ref);
    //return db_query($sql);
    $result = db_query($sql);
    //return $jmlh['jumlah_pesanan'];
    return db_fetch($result);
}

function get_sales_order_details_by_soid_ref($soid_ref)
{
    $sql = "SELECT *
	FROM " . TB_PREF . "sales_order_details
	where ref = " . db_escape($soid_ref);
    $result = db_query($sql);
    return db_fetch($result);
}

function get_bahan_baku_production_card($notapesan)
{
    $sql = "SELECT production_card.item_amount
     , item_codes.description
     , purch_data.price
     , production_card.wo_id
    FROM production_card
    INNER JOIN item_codes
    ON production_card.item_id = item_codes.item_code
    INNER JOIN purch_data
    ON item_codes.stock_id = purch_data.stock_id
    WHERE production_card.wo_id = " . db_escape($notapesan) . " 
    AND production_card.typeid = " . BAG_POTONG;

    return db_query($sql);
}

function get_tenaga_kerja_production_card($notapesan)
{
    $sql = "SELECT sum(production_card.item_amount) AS jmlh
	FROM
	" . TB_PREF . "production_card	
	WHERE production_card.wo_id = " . db_escape($notapesan) . "
	AND	production_card.typeid = " . BAG_JAHIT;
    return db_query($sql);
}


function save_hpp($ref, $bb, $tkl, $bop, $no_nota)
{
    global $Refs;
    $sql = "INSERT INTO " . TB_PREF .
        "hpp (no_nota, ref, bahan_baku, tkl, bop, hpp) 
    VALUES (" . db_escape($no_nota) . ", " . db_escape($ref) . ", " . db_escape($bb) .
        ", " . db_escape($tkl) . ", " . db_escape($bop) . ", " . db_escape($bb + $tkl +
        $bop) . ")";
    $result_ins = db_query($sql);
    if ($result_ins)
    {
        $sql1 = "select hpp_id from hpp where ref = " . db_escape($ref);
        $result = db_query($sql1);
        $no_hpp = db_fetch($result);
        begin_transaction();
        add_gl_trans(HPP, $no_hpp[0], Today(), '61100', 0, 0, "TKL", -$tkl);
        add_gl_trans(HPP, $no_hpp[0], Today(), '61200', 0, 0, "Bahan Baku", -$bb);
        add_gl_trans(HPP, $no_hpp[0], Today(), '64000', 0, 0, "BOP", -$bop);
        add_gl_trans(HPP, $no_hpp[0], Today(), '53000', 0, 0, "HPP", $tkl + $bb + $bop);
        add_audit_trail(HPP, $no_hpp[0], Today(), "Harga Pokok Produksi");
        $Refs->save(HPP, $no_hpp[0], $ref);
        commit_transaction();
    }
}

?>